package com.demo.forum.controller;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.config.AppConfig;
import com.demo.forum.model.Article;
import com.demo.forum.model.ArticleReply;
import com.demo.forum.model.User;
import com.demo.forum.services.impl.ArticleReplyServiceImpl;
import com.demo.forum.services.impl.ArticleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复帖子")
@RestController
@Slf4j
@RequestMapping("/reply")
public class ArticleReplyController {
    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private ArticleReplyServiceImpl articleReplyService;




    @ApiOperation("回复新帖")
    @PostMapping("/create")
    public Result create(HttpServletRequest request,
                         @ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                         @ApiParam("帖子内容") @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_LOGUN);
        //校验用户状态
        if(user.getState() == 1){
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            return Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验帖子是否封贴

        if(article.getState() == 1) {
            return Result.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReplyService.create(articleReply);
        return Result.success();
    }
    @ApiOperation("帖子回复列表")
    @GetMapping("/getReplies")
    public Result<List<ArticleReply>> getRepliesByArticleId(
            @RequestParam("articleId") @ApiParam("帖子id") @NonNull Long articleId){
        Article article = articleService.selectById(articleId);
        if(article == null ||article.getDeleteState() == 1){
            return Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return Result.success(articleReplies);
    }
//@ApiOperation("获取回复列表")
//@GetMapping("/getReplies")
//public Result<List<ArticleReply>> getRepliesByArticleId (@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
//    // 校验帖子是否存在
//    Article article = articleService.selectById(articleId);
//    if (article == null || article.getDeleteState() == 1) {
//        // 返回错误提示
//        return Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
//    }
//    // 调用Service
//    List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
//    // 返回结果
//    return Result.success(articleReplies);
//
//}

}
